package com.zqxq.lottery.api.controller;

import com.zqxq.common.bo.RequestHeaderBO;
import com.zqxq.common.constant.Result;
import com.zqxq.lottery.api.config.FileConfig;
import com.zqxq.lottery.api.feign.LotteryServerFeignClient;
import com.zqxq.lottery.common.utils.CommonUtil;
import com.zqxq.lottery.common.utils.QRCodeUtil;
import com.zqxq.lottery.entity.bo.sys.SysAppVersionBO;
import com.zqxq.lottery.entity.vo.sys.SysAppVersionVO;
import com.zqxq.lottery.entity.vo.user.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


@Api(value = "app安装包接口",tags = "app安装包接口")
@RestController
@Slf4j
@RequestMapping(value = "/app", produces = "application/json")
public class AppController {

    @Autowired
    private FileConfig fileConfig;

    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;

    @ApiOperation("APP更新升级")
    @PostMapping("/upgrade")
    public Result<SysAppVersionVO> queryAppUpgrade(SysAppVersionBO search){
        return lotteryServerFeignClient.appUpgrade(search);
    }

    @ApiOperation("马甲包开关")
    @PostMapping("/which/bag")
    public Result queryWhichBag(RequestHeaderBO search){
        Result rs = new Result();
        rs.setData("1");
        return rs;
        //return lotteryServerFeignClient.queryWhichBag(search);
    }

    @ApiOperation("首页活动开关-1：普通；2：世界杯")
    @PostMapping("/which/activity")
    public Result queryWhichActivity(RequestHeaderBO search){
        Result rs = new Result();
        Result res = lotteryServerFeignClient.queryWhichActivity(search);
        //有数据显示2，没有数据显示1
        if(CommonUtil.isNotEmpty(res) && CommonUtil.isNotEmpty(res.getData()) && res.success()){
            rs.setData("2");
        }else {
            rs.setData("1");
        }
        return rs ;
    }

    @ApiOperation("用户分享二维码")
    @PostMapping("/user/share/rq")
    public Result makeUserRQ(String userId){
        Result rs = new Result();
        Map<String,String> map = new HashMap();
        try {
            String path = "ZQXQ-LOTTERY/image/";
            String image = path+"rq_" + userId+".png";
            File rqFile = new File(fileConfig.getFile_diskPath_public()+image);
            if(!rqFile.exists()) {
                File sourceFile = new File(fileConfig.getFile_diskPath_public() +path+ "cup_rq_bg_img.png");
                String url = "http://www.lanyoushe.cn/gameui/add-land.html?user_id=" + userId;
                QRCodeUtil.makeRQCode(sourceFile, rqFile, url);
            }
            image = "/"+image.replaceAll("\\\\","/");
            map.put("image",image);
        }catch (Exception e){
            log.error("用户【"+userId+"】二维码生成错误：",e);
            map.put("image","/ZQXQ-LOTTERY/image/rq_115213966913044485.png");
        }
        rs.setData(map);
        return rs;
    }

    @ApiOperation("用户分享二维码（订单详情页）")
    @PostMapping("/userorder/share/rq")
    public Result makeUserOrderRQ(String userId){
        Result rs = new Result();
        Map<String,String> map = new HashMap();
        try {
            String path = "ZQXQ-LOTTERY/image/";
            String image = path+"order_" + userId+".png";
            File rqFile = new File(fileConfig.getFile_diskPath_public()+image);
            if(!rqFile.exists()) {
                File sourceFile = new File(fileConfig.getFile_diskPath_public() +path+ "cup_rq_bg_img.png");
                String url = "http://www.lanyoushe.cn/gameui/add-land.html?user_id=" + userId+"&channel=order";
                QRCodeUtil.makeRQCode(sourceFile, rqFile, url);
            }
            image = "/"+image.replaceAll("\\\\","/");
            map.put("image",image);
        }catch (Exception e){
            log.error("用户【"+userId+"】二维码生成错误：",e);
            map.put("image","/ZQXQ-LOTTERY/image/rq_115213966913044485.png");
        }
        rs.setData(map);
        return rs;
    }


}
